<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/7/27
 * Time: 17:16
 */

namespace Admin\Service;


class SheetCommentService
{
    /**
     * 获取曲谱评论
     */
    public function getCommentBySheetId($sheetId)
    {
        $model = M('SheetComment');
        if (empty($sheetId)) {
            //获取所有曲谱评论
            $map = array('is_delete' => 0);
            $list = $model->field('id, sheet_id, user_id, remark, create_time')
                ->where($map)->select();
            //添加谱单名
            $sheetIds = array_column($list, 'sheet_id');
            $sheet = new SheetService();
            $sheetMap = $sheet->getSheetInfoByIds($sheetIds);

            //添加用户昵称
            $userIds = array_column($list,'user_id');
            $user = new UserService();
            $userMap = $user->getUserInfoByIds($userIds);
            foreach ($list as $key => $val) {
                $list[$key]['sheet_name'] = $sheetMap[$val['sheet_id']]['name'];
                $list[$key]['user_name'] = $userMap[$val['user_id']]['nick_name'];
            }
            return $list;

        } else {
            $map = array(
                'sheet_id' => $sheetId,
                'is_delete' => 0
            );
            $list = $model->field('id, sheet_id, user_id, remark, create_time')
                ->where($map)->select();
            //添加谱单名
            $sheetName = M('Sheet')->where(array('id'=>$sheetId))->getField('name');
            //添加用户昵称
            $userIds = array_column($list,'user_id');
            $user = new UserService();
            $userMap = $user->getUserInfoByIds($userIds);
            foreach ($list as $key => $val) {
                $list[$key]['sheet_name'] = $sheetName;
                $list[$key]['user_name'] = $userMap[$val['user_id']]['nickname'];
            }
            return $list;
        }
    }

    /**
     * 获取曲谱评论
     */
    public function getCommentByParam($param)
    {
        $where = array();
        if(!empty($param['sheet_id'])) {
            $sheetIds = $param['sheet_id'];
        }
        if(!empty($param['name'])) {
            $where['name'] = array('like', trim($param['name']).'%');
            $sheetArr = M('sheet')->field('id')->where($where)->select();
            if(empty($sheetArr)) return array();
            $sheetIds = array_column(array_unique($sheetArr), "id");
        }
        
        $model = M('SheetComment');
        //获取所有曲谱评论
        $map['is_delete'] = array('eq', 0);
        if(!empty($sheetIds)) $map["sheet_id"] = array('in', $sheetIds);
        $list = $model->field('id, sheet_id, user_id, remark, create_time')->where($map)->select();
       
        //添加谱单名
        $sheetIds = array_column(array_unique($list), 'sheet_id');
        $sheet = new SheetService();
        $sheetMap = $sheet->getSheetInfoByIds($sheetIds);
        
        //添加用户昵称
        $userIds = array_column($list,'user_id');
        $user = new UserService();
        $userMap = $user->getUserInfoByIds($userIds);
        foreach ($list as $key => $val) {
            $list[$key]['sheet_name'] = $sheetMap[$val['sheet_id']]['name'];
            $list[$key]['user_name'] = $userMap[$val['user_id']]['nickname'];
        }
        return $list;
    }

    /**
     * 获取曲谱评论数
     */
    public function multiCommentCount($sheetIds)
    {
        $model = M('SheetComment');
        $map = array(
            'sheet_id' => array('in', $sheetIds),
            'is_delete' => 0
        );

        $res = $model->field('sheet_id, count(*)')->where($map)->group('sheet_id')->select();
        $commentCountMap = array();
        foreach ($res as $key => $val) {
            $commentCountMap[$val['sheet_id']] = $val['count(*)'];
        }
        return $commentCountMap;
    }

    /**
     * 添加曲谱评论
     * @param $param
     * @return mixed
     */
    public function add($param)
    {
        $model = M('SheetComment');
        $data = $model->create($param);
        if ($data) {
            //补充参数
            $param['user_id'] = $param['uid'];
            $id = $model->add($param);

            //曲谱评论数 +1
            M('Sheet')->where(array('id'=>$param['sheet_id']))->setInc('comment_count');

            return $id;
        } else {
            E($model->getError());
        }
    }
}